"""
Case Type   : 数据库系统
Case Name   : LocalDate类型geiObject并发用例
Create At   : 2021/04/25
Owner       : opentestcase026
Description :
    step1: 创建表并插入数据;
    step2: 编译java工具;
    step3: 运行java工具;
Expect      :
    step1: 创建表并插入数据; expect:插入成功
    step2: 编译java工具; expect:成功
    step3: 运行java工具; expect:结果正确
History     :
    Modified by opentestcase001 2021/06/10: 修改断言方式，规避数据库主节点与yat执行机存在时间差问题
    Modified by opentestcase024 2023/11/17：优化并发执行方法，使用java脚本并发
"""
import datetime
import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class JDBCSetGetObject(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.pri_dbuser = Node(node='PrimaryDbUser')
        self.pri_root = Node(node='PrimaryRoot')
        self.log.info("----this is setup----")
        self.log.info(f'----{os.path.basename(__file__)}:start----')
        self.target_path = os.path.join(macro.DB_BACKUP_PATH, 'JDBC_test')
        self.java_name = "jdbc_set_get_object_case0015"
        self.tb_name = "jdbc_set_get_object_case0015"
        self.common = Common()
        self.constant = Constant()
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.thread_num = 20

    def test_index(self):
        step_txt = '----step1: 创建表并插入数据; expect:插入成功----'
        self.log.info(step_txt)
        cmd = f"drop table if exists {self.tb_name};" \
            f"create table {self.tb_name}(t_date date);"
        result = self.pri_sh.execut_db_sql(cmd)
        self.log.info(result)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, result)
        cmd = f"insert into {self.tb_name} values('now');" \
            f"insert into {self.tb_name} values('2020-02-29');" \
            f"insert into {self.tb_name} values('2020-03-01');" \
            f"insert into {self.tb_name} values('epoch');"
        result = self.pri_sh.execut_db_sql(cmd)
        self.log.info(result)
        self.assertIn('INSERT', result, "执行失败" + step_txt)

        step_txt = '----step2: 编译java工具; expect:成功----'
        self.log.info(step_txt)
        self.common.scp_file(self.pri_root,
                             f"{self.java_name}.java", self.target_path)
        self.pri_root.scp_put(macro.JDBC_PATH,
                              f"{self.target_path}/postgresql.jar")
        cmd = f"javac -encoding utf-8 -cp " \
            f"{os.path.join(self.target_path, 'postgresql.jar')} " \
            f"{os.path.join(self.target_path, f'{self.java_name}.java')}"
        self.log.info(cmd)
        result = self.pri_root.sh(cmd).result()
        self.log.info(result)
        self.assertEqual('', result, "执行失败" + step_txt)

        step_txt = '----step3: 运行java工具; expect:结果正确----'
        self.log.info(step_txt)
        result_time = self.pri_root.sh(
            "date '+%Y-%m-%d'").result()
        self.log.info(result_time)
        today = (datetime.datetime.strptime(result_time, '%Y-%m-%d')
                 ).strftime('%Y-%m-%d')
        url_host = f"{self.pri_dbuser.db_host}:{self.pri_dbuser.db_port}"
        java_cmd = f"java -cp " \
            f"{os.path.join(self.target_path, 'postgresql.jar')}" \
            f":{self.target_path} {self.java_name} {url_host} " \
            f"{self.pri_dbuser.db_name} {self.pri_dbuser.db_user} " \
            f"{self.pri_dbuser.db_password} {self.tb_name} {self.thread_num}"
        java_result = self.common.get_sh_result(
            self.pri_root, java_cmd)
        self.assertEqual(java_result.count(f"第0行结果：{today}"),
                         self.thread_num, "执行失败" + step_txt)
        self.assertEqual(java_result.count(f"第1行结果：2020-03-01"),
                         self.thread_num, "执行失败" + step_txt)
        self.assertEqual(java_result.count(f"第2行结果：2020-02-29"),
                         self.thread_num, "执行失败" + step_txt)
        self.assertEqual(java_result.count(f"第3行结果：1970-01-01"),
                         self.thread_num, "执行失败" + step_txt)

    def tearDown(self):
        self.log.info('----this is tearDown----')
        step_txt = '----清理环境; expect:结果正确----'
        self.log.info(step_txt)
        cmd = f"drop table if exists {self.tb_name};"
        result = self.pri_sh.execut_db_sql(cmd)
        self.log.info(result)
        cmd = f"rm -rf {self.target_path}"
        self.log.info(cmd)
        self.pri_root.sh(cmd)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, result,
                      "执行失败" + step_txt)
        self.log.info(f'----{os.path.basename(__file__)}:end----')
